package weiqian.hardware;

import android.util.Log;

import java.util.concurrent.atomic.AtomicBoolean;


public class SerialPortManager {

    private final AtomicBoolean mSerialPortStatus = new AtomicBoolean(false);
    private final AtomicBoolean mThreadStatus = new AtomicBoolean(false);
    public SerialPort mSerialPort = null;
    private ReadThread mReadThread;
    public OnDataReceiveListener onDataReceiveListener = null;


    public SerialPortManager() {
        mSerialPort = new SerialPort();


    }

    public void open(String path, int baud, int databits, String parity, int stopbits) {
        if (mSerialPort != null) {
            try {
                mSerialPort.open(path, baud, databits, parity, stopbits);
                int fd = mSerialPort.getFd();
                boolean success = fd != -1;
                Log.i("SerialPortManager", "open == : 打开串口" + (success ? "成功" : "失败"));
            } catch (Exception e) {
                Log.e("SerialPortManager", "open == : 打开串口异常：" + e);
            }

            this.mSerialPortStatus.set(true);
            this.mThreadStatus.set(false);
            this.mReadThread = new ReadThread();
            this.mReadThread.setName("Recv Thread");
            this.mReadThread.start();
        }
    }


    public void closeSerialPort() {
        try {
            if (this.mSerialPortStatus.get()) {
                this.mSerialPortStatus.set(false);
                this.mThreadStatus.set(true);
                this.mSerialPort.close();
                Log.i("SerialPortManager", "closeSerialPort == : 关闭串口成功");
            }

        } catch (Exception var2) {
            Log.e("SerialPortManager", "closeSerialPort == : 关闭串口异常：" + var2);
        }
    }

    public void sendPacket(byte[] sendData) {
        try {
            if (sendData.length > 0 && this.mSerialPortStatus.get()) {
                mSerialPort.write(sendData, sendData.length);
            }
        } catch (Exception var3) {
            Log.e("SerialPortManager", "sendSerialPort == : 串口数据发送失败：" + var3);
        }
    }


    public void setOnDataReceiveListener(OnDataReceiveListener dataReceiveListener) {
        this.onDataReceiveListener = dataReceiveListener;
    }


    public interface OnDataReceiveListener {
        void onDataReceive(byte[] var1, int var2);
    }

    private class ReadThread extends Thread {
        private ReadThread() {
        }

        @Override
        public void run() {

            while (!SerialPortManager.this.mThreadStatus.get() && !this.isInterrupted()) {
//                int size = false;

                try {
                    byte[] buffer = new byte[1024];
                    mSerialPort.read(buffer, 1024);
                    if (buffer[0] == 0 && buffer[1] == 0) {
                        continue;
                    }
                    byte[] readBytes = new byte[64];
                    System.arraycopy(buffer, 0, readBytes, 0, 64);
                    if (SerialPortManager.this.onDataReceiveListener != null) {
                        SerialPortManager.this.onDataReceiveListener.onDataReceive(readBytes, 64);
                    }
                } catch (Exception var4) {
                    Log.e("SerialPortManager", "run == : 数据读取异常：" + var4);
                }
            }
        }
    }

}
